//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    // 组件所需的参数
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的', //导航栏 中间的标题
      height: 0
    },
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    userId: -1,
    message: {},
    showDialog1: false,
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
    isLoading: false
  },
  onLoad() {
    this.setData({
      height: app.globalData.height
    })
    var that = this
    wx.getStorage({
      key: 'userId',
      success: function(res) {
        that.setData({
          userId: res.data
        })
        app.globalData.userId = res.data
      },
      fail: function() {
        if (that.data.userId == -1) {
          that.setData({
            showDialog1: true
          })
        }
      }
    })
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          userInfo: res.data
        })
      },
      fail: function() {
        if (that.data.userId == -1) {
          that.setData({
            showDialog1: true
          })
        }
      }
    })
    that.setData({
      userInfo: wx.getStorageSync('userInfo'),
      userId: wx.getStorageSync('userId')
    })
  },
  checkAdmin() {
    let that = this
    app.call({
      path: '/user/check',
      data: {'userId':new Number(that.data.userId)},
      method: "post",}).then(res=>{
        console.log('checkAdmin：'+JSON.stringify(res))
        if (res.data.data.userIsAdmin != 2) {
          wx.showModal({
            title: '提示',
            content: '还未获得权限，请联系管理员',
          })
        }
        if (res.data.data.userIsAdmin == 2) {
          wx.showModal({
            title: '提示',
            content: '已获得管理员权限',
          })
        }
        wx.setStorage({
          key: 'userIsAdmin',
          data: res.data.data.userIsAdmin,
        })
        app.globalData.userIsAdmin = res.data.data.userIsAdmin
      })
    },
  getUserProfile:function(e){
    let that = this
    wx.getUserProfile({
      desc: '采集微信基本信息如头像等', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        wx.showLoading({
          title: '登陆中~',
        })
        console.log(res)
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        var list = {
          "nickName": that.data.userInfo.nickName,
          "gender": that.data.userInfo.gender,
          "city": that.data.userInfo.city,
          "province": that.data.userInfo.province,
          "country": that.data.userInfo.country,
          "avatarUrl": that.data.userInfo.avatarUrl
        }
    
        var listMessage = {
          "userAvatar": that.data.userInfo.avatarUrl,
          "userNickname": that.data.userInfo.nickName,
          "userGender": that.data.userInfo.gender,
        }
        app.call({
          path: '/user/wx8cdb755f6ba3a118/info',
          method: 'post',
          data: listMessage,
          content: 'application/json;charset=UTF-8'
        }).then(res=>{
          wx.hideLoading();
          if (res.statusCode != 200) {
            console.log('userInfo：'+JSON.stringify(res))
            wx.showModal({
              title: '提示',
              content: '出现问题啦，在试一下吧~',
            })
            return;
          }
          that.setData({
            userId: res.data.data,
            userInfo: list,
          })
          wx.setStorage({
            key: 'userId',
            data: res.data.data,
          })
          app.globalData.userId = res.data.data
          wx.setStorage({
            key: 'userInfo',
            data: list,
          })
          app.globalData.userInfo = list
          that.setData({
            showDialog1: false
          })
          },()=>{
            wx.hideLoading();
          })
        }
    })
    // wx.login({
    //   success: function(res) {
    //     wx.showLoading({
    //       title: '登陆中~',
    //     })
    //     // console.log(that.data.openid)
    //     // 获取登录的临时凭证
    //     var code = res.code;
    //     // 调用后端，获取微信的session_key, secret
    //     app.call({
    //       path: "/wx8cdb755f6ba3a118/login?code=" + code,
    //       method: "POST",
    //       data: JSON.stringify(listMessage),
    //       dataType: JSON,}).then(res=>{
    //         wx.hideLoading();
    //         if (res.statusCode != 200) {
    //           console.log(res)
    //           wx.showModal({
    //             title: '提示',
    //             content: '出现问题啦，在试一下吧~',
    //           })
    //           return;
    //         }
    //         that.setData({
    //           message: JSON.parse(res.data),
    //           userInfo: list,
    //         })
    //         wx.setStorage({
    //           key: 'userId',
    //           data: that.data.message.userId,
    //         })
    //         wx.setStorage({
    //           key: 'userInfo',
    //           data: list,
    //         })

    //         app.globalData.userId = that.data.message.userId

    //         app.globalData.userInfo = list


    //         if (that.data.message.code === 200) {
    //           wx.showModal({
    //             title: '提示',
    //             content: '欢迎回来，老朋友~',
    //           })
    //         }
    //         if (that.data.message.code === 300) {
    //           wx.showModal({
    //             title: '提示',
    //             content: '你好新朋友，快去探索吧~',
    //           })
    //         }
    //         that.setData({
    //           showDialog1: false
    //         })
    //       })
    //   }
    // })
  },
  onReady() {
    let that = this
    setTimeout(function() {
      that.setData({
        isLoading: true
      })
    }, 500)
  },

  attention() {
    wx.showModal({
      title: '提示',
      content: '公众号名称 暂无',
      confirmText: "复制",
      showCancel: false,
      success: function() {
        wx.setClipboardData({
          data: '',
        })
      }
    })
  },
  call() {
    wx.showModal({
      title: '提示',
      content: '微信联系/手机联系',
      confirmText: "手机联系",
      confirmColor: "#3cc",
      cancelColor: "#3cc",
      cancelText: "微信联系",
      success: function(e) {
        if (e.confirm) {
          wx.showModal({
            title: '提示',
            content: '是否联系(18515063697)',
            success: function(e) {
              if (e.confirm) {
                wx.makePhoneCall({
                  phoneNumber: '18515063697',
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '微信号：18515063697',
            confirmText: "复制",
            success: function(e) {
              if (e.confirm) {
                wx.setClipboardData({
                  data: '18515063697',
                })
              }
            }
          })
        }
      }
    })
  },
  onShow() {
    let that = this
    this.setData({
      userId: app.globalData.userId
    })
    wx.getStorage({
      key: 'userId',
      success: function(res) {
        that.setData({
          userId: res.data
        })
      },
    })
  }
})